﻿
using Business.Base.Utils;
using Business.Base.Model;
using System;
using System.Collections.Generic;

using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Pumptester.ExperimentForms.Base
{
    public partial class BaseCreateForm : Form
    {

        #region 变量和属性
        Experiment experiment;

        //BaseService service;

        protected List<TextBox> notNullList;
        protected List<TextBox> doubleFormateList;
        protected List<TextBox> doubleorNullFormateList;
        protected List<TextBox> intList;



        public static Regex RegNumber = new Regex("^[0-9]+$");
        public static Regex RegNumberSign = new Regex("^[+-]?[0-9]+$");
        public static Regex RegDecimal = new Regex("^[0-9]+[.]?[0-9]+$");
        public static Regex RegDecimalSign = new Regex("^[+-]?[0-9]+[.]?[0-9]+$"); //等价于^[+-]?\d+[.]?\d+$
        public static Regex RegEmail = new Regex(@"^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$");//w 英文字母或数字的字符串，和 [a-zA-Z0-9] 语法一样 
        public static Regex RegCHZN = new Regex("[\u4e00-\u9fa5]");


        public Experiment Experiment { get => experiment; set => experiment = value; }

        #endregion

        #region 构造函数
        public BaseCreateForm()
        {
            InitializeComponent();
            
        }
        #endregion

        /// <summary>
        /// 将可选的数据绑定Combbox
        /// </summary>
        protected virtual void BandCombox()
        { 

        }

        /// <summary>
        /// 根据创建的数据构建试验对象
        /// </summary>
        /// <returns></returns>
        protected virtual Experiment buildExperiment()
        {
            return null;
        }

        

        #region 读取和保存最后一次的测试数据

        protected virtual String RecentRecordFilePath => "";
 
        protected virtual void assignRecentRecordToFace(Experiment experiment)
        {
            
        }

        /// <summary>
        /// 读取最近的记录
        /// </summary>
        private void readRecentRecord()
        {
            String file = RecentRecordFilePath;
            if (file == null || file.Length == 0)
            {
                return ;
            }

            Experiment experiment = (Experiment)SerializationUtils.Read(file);
            if (experiment == null)
            {
                return;
            }

            assignRecentRecordToFace(experiment);
        }

        /// <summary>
        /// 保存最新结果
        /// </summary>
        /// <param name="experiment"></param>
        private void saveRecord(Experiment experiment)
        {
            String file = RecentRecordFilePath;
            if (file == null || file.Length == 0)
            {
                return;
            }
            SerializationUtils.Write(file, experiment);
        }
        #endregion

        #region 创建时的验证框架
        protected virtual void initValidateInfo()
        {

        }
        /// <summary>
        /// 验证控件的数据
        /// </summary>
        /// <param name="c"></param>
        /// <returns></returns>
        protected virtual Boolean ValidateContent(Control c)
        {
            //this.errorProvider.Clear();
            Boolean ret = true;
            foreach (Control control in c.Controls)
            {
                if (control.Controls.Count > 1)
                {
                    ret = ValidateContent(control);
                }
                else
                {
                    if (control is TextBox)
                    {
                        TextBox box = (TextBox)control;
                        if (notNullList.Contains(box))
                        {
                            if (box.Text.Trim() == "")
                            {
                                this.errorProvider.SetError(box, "非空,请填写内容");
                                ret = false;
                                continue;
                            }
                        }
                        if (intList.Contains(box))
                        {
                            if (!RegNumber.IsMatch(box.Text.Trim()))
                            {
                                this.errorProvider.SetError(box, "请填写整数");
                                ret = false;
                                continue;
                            }

                        }
                        if (doubleFormateList.Contains(box))
                        {
                            if (!RegDecimal.IsMatch(box.Text.Trim()))
                            {
                                this.errorProvider.SetError(box, "请填写浮点数据");
                                ret = false;
                                continue;
                            }
                        }
                        if (doubleorNullFormateList.Contains(box))
                        {
                            if (box.Text.Trim().Length != 0 && !RegDecimal.IsMatch(box.Text.Trim()))
                            {
                                this.errorProvider.SetError(box, "请填写浮点数据");
                                ret = false;
                                continue;
                            }
                        }
                    }
                }

            }
            return ret;
        }
        #endregion

        #region 界面事件触发的函数

        

        private void btnSure_Click(object sender, EventArgs e)
        {
            this.errorProvider.Clear();
            if (this.ValidateContent(this))
            {
                Experiment = buildExperiment();
                if (Experiment != null)
                {
                    saveRecord(Experiment);
                }
                this.DialogResult = DialogResult.OK;
            }
        }

        private void btnCancle_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;

        }

        private void BaseCreateForm_Load(object sender, EventArgs e)
        {
            initValidateInfo();
            BandCombox();
            readRecentRecord();
        }
        #endregion

      
    }
}
